//
//  EmojiMemoryGameView.swift
//  Memorize
//
//  Created by YJH on 2025/4/16.
//

import SwiftUI

struct  EmojiMemoryGameView: View {
    @ObservedObject var viewModel: EmojiMemoryGame
    //    let emojis: [String] = ["👌","❤️","😳","😉"]
    //    let emojis: Array<String> = ["👌","❤️","😳","😉"]
    let emojis = ["👌","❤️","😳","😉","❤️","😳","😉","❤️","😳","😉"]
    
    var body: some View {
        VStack{
            ScrollView{
                cards
                    .animation(.default, value: viewModel.cards)
            }
            Button("Shuffle"){
                viewModel.shuffle()
            }
        }
     
        .padding()
    }
    var cards: some View{
        LazyVGrid(columns: [GridItem(.adaptive(minimum: 85),spacing: 0)],spacing: 0) {
            //            0..<4 = 0,1,2,3   0...4 = 0,1,2,3,4
            ForEach(viewModel.cards,){ card in
//                VStack(spacing: 0){
                    CardView(card)
                        .aspectRatio ( 2/3, contentMode: .fit)
                        .padding(4)
                        .onTapGesture {
                            viewModel.choose(card)
                        } 
//                    Text(card.id)
//                }
                        

            }
            .foregroundColor(.orange)
        }
    }
}




struct CardView: View {
    let card: MemoryGame<String>.Card
    
    init(_ card: MemoryGame<String>.Card) {
        self.card = card
    }
     
    var body: some View {
        ZStack {
         let base = RoundedRectangle(cornerRadius: 12)
            Group {
                base.fill(.white)
                base.strokeBorder(lineWidth: 2)
                Text(card.content)
                    .font(.system(size: 200))
                    .minimumScaleFactor(0.01)
                    .aspectRatio(1,contentMode: .fit) 
            }
            .opacity(card.isFaceUp ? 1 : 0)
            base.fill().opacity(card.isFaceUp ? 0 : 1)
                
            }
        .opacity(card.isFaceUp || !card.isMatched ? 1 : 0)
    }
}

#Preview {
    EmojiMemoryGameView(viewModel: EmojiMemoryGame())
}
